//
//  UITabBar+BadgeTop.m
//  shh
//
//  Created by DS on 2018/3/31.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "UITabBar+BadgeTop.h"

#define TabbarItemNums 5.0    //tabbar的数量

@implementation UITabBar (BadgeTop)

/**
 展示小红点
 @param index 下标
 */
-(void)showBadgeOfIndexTop:(NSInteger)index WithCount:(NSString *)count{
    [self removeBadgeOfIndex:index];
    
    UILabel * badgeV = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 12, 10)];
    //UIView *badgeV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    //UIView *badgeV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, 7)];
    badgeV.tag = 999+index;
    badgeV.layer.cornerRadius = 5;
   // badgeV.layer.cornerRadius = 3.5;
    badgeV.clipsToBounds = YES;
    badgeV.backgroundColor = getColor(whiteColor);
    badgeV.text = [NSString stringWithFormat:@"%@",count];
    badgeV.textColor = getColor(@"FF2828");
    badgeV.font = DEF_FontSize_8;
    badgeV.textAlignment = NSTextAlignmentCenter;
    badgeV.layer.borderColor = [UIColor redColor].CGColor;
    badgeV.layer.borderWidth = 1;
    
    //确定小红点的位置
    CGRect tabFrame = self.frame;
    float percentX = (index +0.6) / TabbarItemNums;
    //float percentX = (index +0.55) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    //CGFloat y = ceilf(0.5 * tabFrame.size.height);
    badgeV.frame = CGRectMake(x, y, 30, 12);
    //badgeV.frame = CGRectMake(x, y, 7, 7);
    
    //    NSLog(@"the badge new frame is %@",NSStringFromCGRect(badgeV.frame));
    [self addSubview:badgeV];
}

/**
 隐藏小红点
 @param index 下标
 */
-(void)hiddenBadgeOfIndexTop:(NSInteger)index{
    [self removeBadgeOfIndex:index];
}

/**
 移除小红点
 @param index 下标
 */
-(void)removeBadgeOfIndex:(NSInteger)index{
    for (UIView *view in self.subviews) {
        if (view.tag == 999+index) {
            [view removeFromSuperview];
        }
    }
}




@end
